﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using AssetsSys.BLL.AutoMapper;
using AssetsSys.DAL;
using AssetsSys.Dto;
using AssetsSys.Dto.Assets;
using AssetsSys.Dto.Modify;
using AssetsSys.Dto.Status;
using AssetsSys.model;

namespace AssetsSys.BLL
{
    /// <summary>
    /// 变更记录
    /// </summary>
    public class ModifyManger
    {
        /// <summary>
        /// 获取变更列表
        /// </summary>
        /// <returns></returns>
        public static async Task<PageModel> GetList(ModifyQuertDto request, Guid userId)
        {
            using (var modSvc = new ModifyService())
            {
                Expression<Func<Modify, bool>> predicate = (x => x.IsRemoved == false
                                                                && (request.NewStatusName == null || x.NewState.StatusName.Contains(request.NewStatusName))
                                                                && (request.OldStatusName == null || x.OldState.StatusName.Contains(request.OldStatusName))
                                                                && (request.AssetsNum == null || x.Assets.AssetsNum.Contains(request.AssetsNum))
                                                                && (request.ModifyState == null || x.ModifyState == request.ModifyState)
                                                                && (request.AssetsName == null || x.Assets.AssetsName.Contains(request.AssetsName))
                                                                && (x.AcceptId == userId || x.ModifyId == userId)
                );
                var totalCount = (await modSvc.GetAll(predicate, true).ToListAsync()).Count;
                var department = await modSvc.GetAll(predicate, false, request.PageIndex, request.PageSize).Select(x => new ModifyResponseModel()
                {
                    Id = x.Id,
                    AssetsName = x.Assets.AssetsName,
                    AssetsNum = x.Assets.AssetsNum,
                    OldStatus = x.OldState.StatusName,
                    NewStatus = x.NewState.StatusName,
                    ModifyState = x.ModifyState,
                    AcceptName = x.AcceptUsers.UserName,
                    AcceptDepartment = x.AcceptUsers.Department.DepartmentName,
                    ModifyDescribe = x.ModifyDescribe,
                    ModifyName = x.ModifyUsers.UserName,
                    ModifyDepartment = x.ModifyUsers.Department.DepartmentName,
                    CreateTime = x.CreateTime,
                    IsForMe = x.AcceptId == userId
                }).ToListAsync();
                return new PageModel()
                {
                    Count = totalCount,
                    Data = department
                };
            }
        }

        /// <summary>
        /// 添加变更
        /// </summary>
        /// <param name="assetsId"></param>
        /// <param name="newStateId"></param>
        /// <param name="modifyDescribe"></param>
        /// <param name="acceptId"></param>
        /// <param name="modifyId"></param>
        /// <param name="isChange"></param>
        /// <returns></returns>
        public static async Task<bool> AddModify(Guid assetsId, Guid newStateId, string modifyDescribe, Guid acceptId, Guid modifyId, int isChange = 1)
        {
            Assets asstes;
            using (var assSvc = new AssetsService())
            {
                asstes = await assSvc.GetOne(assetsId);
            }
            if (isChange == 1)
            {
                var asseteModel = new AssetsModel()
                {
                    Id = assetsId,
                    StatusId = Guid.Parse("82250994-EECD-4F1D-BADF-D5E61CC76638"),
                };
                await AssetsManger.Update(asseteModel);
            }
            using (var modSvc = new ModifyService())
            {
                await modSvc.CreateAsync(new Modify()
                {
                    AssetsId = assetsId,
                    OldStateId = asstes.StatusId,
                    NewStateId = newStateId,
                    ModifyDescribe = modifyDescribe,
                    AcceptId = acceptId,
                    ModifyId = modifyId,
                    ModifyState = isChange == 0 ? "1" : "0",

                });
                return true;
            }
        }

        /// <summary>
        /// 完成变更
        /// </summary>
        /// <param name="id"></param>
        /// <param name="modifyState"></param>
        /// <returns></returns>
        public static async Task<bool> Update(Guid id, string modifyState)
        {
            using (var modSvc = new ModifyService())
            {
                var oldModel = await modSvc.GetOne(id);
                if (oldModel == null)
                {
                    return false;
                }
                oldModel.ModifyState = modifyState;
                oldModel.ModifyDescribe = oldModel.ModifyDescribe;
                await modSvc.EditAsvnc(oldModel);
                var asseteModel = new AssetsModel()
                {
                    Id = oldModel.AssetsId,
                    StatusId = oldModel.NewStateId,
                    PrincipalId = modifyState == "0" ? oldModel.AcceptId : oldModel.ModifyId,
                };
                await AssetsManger.Update(asseteModel);
            }
            return true;
        }
    }
}
